package cn.zb.controller;

import cn.zb.api.flow.vo.MyTaskVO;
import cn.zb.api.user.dto.QjDTO;
import cn.zb.common.annotation.DictQuery;
import cn.zb.common.util.RedisUtil;
import cn.zb.common.util.Result;
import cn.zb.entity.FlowQj;
import cn.zb.entity.auth.SysUser;
import cn.zb.mapper.FlowQjMapper;
import cn.zb.service.FlowQjService;
import io.swagger.annotations.ApiOperation;
import org.dromara.warm.flow.core.dto.FlowParams;
import org.dromara.warm.flow.core.entity.Instance;
import org.dromara.warm.flow.core.entity.Task;
import org.dromara.warm.flow.core.entity.User;
import org.dromara.warm.flow.core.enums.SkipType;
import org.dromara.warm.flow.core.exception.FlowException;
import org.dromara.warm.flow.core.service.DefService;
import org.dromara.warm.flow.core.service.InsService;
import org.dromara.warm.flow.core.service.TaskService;
import org.dromara.warm.flow.core.service.UserService;
import org.dromara.warm.flow.orm.entity.FlowTask;
import org.dromara.warm.flow.orm.entity.FlowUser;
import org.dromara.warm.flow.ui.service.HandlerSelectService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

/**
 * 请假控制器
 *
 * @author:
 * @date: 2024-11-25 15:11
 **/
@RestController
@RequestMapping("qj")
public class QjController {
    @Resource
    FlowQjService qjService;

    @Resource
    RedisUtil redisUtil;
    @Resource
    InsService insService;
    @Resource
    TaskService taskService;
    @Resource
    UserService userService;

    @PostMapping("sq")
    @ApiOperation("请假任务申请")
    public Result sq(@RequestBody FlowQj qj,
                     @RequestHeader("token") String token){
        // 添加请假流程业务表
        SysUser user = (SysUser) redisUtil.get("login:"+token);
        qj.setUid(user.getUserId());
        qjService.save(qj);
        // 开启任务
        FlowParams test = FlowParams.build().flowCode("test");
        // 设置创建者
        test.handler(user.getNickName());
        test.message(qj.getContent());
        // 开启流程实例
        insService.start(String.valueOf(qj.getId()), test);
        return Result.ok("提交成功");
    }


    @PostMapping("submit")
    @ApiOperation("根据流程实例id开启任务")
    public Result submit(@RequestBody QjDTO dto, @RequestHeader("token") String token){
        SysUser user = (SysUser) redisUtil.get("login:"+token);
        List<String> list = new ArrayList<>();
        // 给与角色权限
        user.getRoles().forEach(r->{list.add("role:"+r);});
        // 给与用户权限
        list.add("user:"+user.getUserId());
        // 给与部门权限
        list.add("dept:"+user.getDeptId());

        FlowParams params = FlowParams.build()
                .skipType(SkipType.PASS.getKey())
                .permissionFlag(list);
        // 流转参数
        Map<String,Object> map = new HashMap<>();
        map.put("day",dto.getDay());
        params.variable(map);
        try {
            insService.skipByInsId(dto.getInsId(), params);
            return Result.ok("任务提交成功");
        } catch (FlowException e) {
            e.printStackTrace();
            return Result.fail("任务提交失败,您不具备该申请权限");
        }
    }

    @Resource
    FlowQjMapper qjMapper;
    @PostMapping("mytask")
    @ApiOperation("查询我的请假任务")
    @DictQuery
    public Result<List<MyTaskVO>> myTask(@RequestHeader("token") String token){
        SysUser user = (SysUser) redisUtil.get("login:"+token);
        return Result.data(qjMapper.myTask(user.getUserId()));
    }
}
